﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using JX.Application;
using JX.Core.Entity;
using JX.Infrastructure;
using JX.Infrastructure.Common;
using JX.Infrastructure.Field;
using JX.Infrastructure.Framework.Authorize;
using JXWebSite.Areas.Admin.Models.UserViewModels;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace JXWebSite.Areas.Admin.Controllers
{
	/// <summary>
	/// 财务管理模块控制器
	/// </summary>
	[Area("Admin")]
	[Route("Admin/[controller]/[action]")]
	[AdminAuthorize]
	public class FinanceController : Controller
	{
		private IBankrollLogServiceAppDapper _BankrollLogService;
		private IExpLogServiceAppDapper _ExpLogService;
		private IPointLogServiceAppDapper _PointLogService;
		private IValidLogServiceAppDapper _ValidLogService;
		private IClientServiceAppDapper _ClientService;
		private IPayPlatFormServiceAppDapper _PayPlatFormService;
		private IPaymentLogServiceAppDapper _PaymentLogService;
		private IOrdersServiceAppDapper _OrdersService;
		private IMentionLogServiceAppDapper _MentionLogService;
		private IMentionServiceAppDapper _MentionService;
		/// <summary>
		/// 依赖注入
		/// </summary>
		/// <param name="ClientService"></param>
		/// <param name="PayPlatFormService"></param>
		/// <param name="PaymentLogService"></param>
		/// <param name="OrdersService"></param>
		/// <param name="MentionLogService"></param>
		/// <param name="MentionService"></param>
		/// <param name="BankrollLogService"></param>
		/// <param name="ExpLogService"></param>
		/// <param name="PointLogService"></param>
		/// <param name="ValidLogService"></param>
		public FinanceController(IClientServiceAppDapper ClientService,
			IPayPlatFormServiceAppDapper PayPlatFormService,
			IPaymentLogServiceAppDapper PaymentLogService,
			IOrdersServiceAppDapper OrdersService,
			IMentionLogServiceAppDapper MentionLogService,
			IMentionServiceAppDapper MentionService,
			IBankrollLogServiceAppDapper BankrollLogService,
			IExpLogServiceAppDapper ExpLogService,
			IPointLogServiceAppDapper PointLogService,
			IValidLogServiceAppDapper ValidLogService)
		{
			_ClientService = ClientService;
			_PayPlatFormService = PayPlatFormService;
			_PaymentLogService = PaymentLogService;
			_OrdersService = OrdersService;
			_MentionLogService = MentionLogService;
			_MentionService = MentionService;
			_BankrollLogService = BankrollLogService;
			_ExpLogService = ExpLogService;
			_PointLogService = PointLogService;
			_ValidLogService = ValidLogService;
		}

		#region 资金明细
		/// <summary>
		/// 资金明细列表页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,BankrollItemList")]
		public IActionResult BankrollItemList()
		{
			return View();
		}

		/// <summary>
		/// 得到资金明细的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,BankrollItemList")]
		public IActionResult GetBankrollItemList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			string SearchName = Utility.Query("SearchName");
			string SearchKeyword = Utility.Query("SearchKeyword");
			int MoneyStatus = Utility.Query("Status", -1);
			int TabStatus = Utility.Query("TabStatus", 0);
			string BeginDate = Utility.Query("BeginDate");
			string EndDate = Utility.Query("EndDate");
			string filter = " 1=1 ";
			if (!string.IsNullOrEmpty(SearchKeyword))
			{
				if(SearchName == "ClientID")//客户名称
				{
					filter += " and B." + SearchName + " in (select ClientID from Client where ClientName like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%')";
				}
				else if (SearchName == "OrderID")//跟单员
				{
					filter += " and B." + SearchName + " IN (SELECT OrderID FROM Orders WHERE Functionary like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%')";
				}
				else
				{
					filter += " and B." + SearchName + " like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%'";
				}
			}
			if (!string.IsNullOrEmpty(BeginDate))
			{
				DateTime now;
				if (DateTime.TryParse(BeginDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',DateAndTime) >= 0 ";
				}
			}
			if (!string.IsNullOrEmpty(EndDate))
			{
				DateTime now;
				if (DateTime.TryParse(EndDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',DateAndTime) <= 0 ";
				}
			}
			switch (MoneyStatus)
			{
				case 0://未确认
				case 1://已确认
					filter += " and B.Status=" + MoneyStatus;
					break;
			}
			switch (TabStatus)
			{
				case 1://收入
					filter += " and B.Money >= 0";
					break;
				case 2://支出
					filter += " and B.Money < 0";
					break;
			}
			string PrimaryColumn = "B.ItemID";
			string strColumn = " B.*, C.ShortedForm ,C.ClientName,(select Functionary from Orders O where O.OrderID=B.OrderID) as Functionary ";
			string TableName = "BankrollLog B LEFT JOIN Client C ON B.ClientID = C.ClientID";
			string SortColumnDbType = "DateTime";
			string SortColumn = "DateAndTime";
			int RecordTotal;
			var result = _BankrollLogService.GetListPagedBySortColumn<dynamic>(startRowIndexId: (PageNum - 1) * PageSize, maxNumberRows: PageSize,
				PrimaryColumn: PrimaryColumn, StrColumn: strColumn, TableName: TableName, Filter: filter,
				SortColumnDbType: SortColumnDbType, SortColumn: SortColumn, Sorts: "desc", out RecordTotal).ToList();
			string strSQL = "select ISNULL(SUM(money), 0) from "+ TableName + " where " + filter;
			var totalIncome = _BankrollLogService.GetBySQL<decimal>(strSQL + " and B.Money >= 0 and B.Status=1");
			var totalPayout = _BankrollLogService.GetBySQL<decimal>(strSQL + " and B.Money < 0 and B.Status=1");
			var pagerModel = new PagerModel<dynamic>(PageNum, PageSize, RecordTotal, result);
			var tempEntity = new { pagerModel.PageNum, pagerModel.PageSize, pagerModel.RecordTotal, pagerModel.PageCount, pagerModel.RowList, TotalIncome=totalIncome.ToString("0.00"), TotalPayout=totalPayout.ToString("0.00") };
			return Json(tempEntity);
		}

		/// <summary>
		/// 删除资金明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,BankrollItemList")]
		public IActionResult DelBankroll(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if(_BankrollLogService.Get(id).Status == 1)
				{
					return Json(new
					{
						Result = "删除失败！不能删除已确认的记录！"
					});
				}
				if (_BankrollLogService.Delete(id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除资金明细
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,BankrollItemList")]
		public IActionResult DelBankrollMulti(string ids)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			if (!DataValidator.IsValidId(ids))
			{
				return Json(new
				{
					Result = "删除失败！指定要删除的记录ID格式不对！"
				});
			}
			try
			{
				var arrIDs = StringHelper.GetArrayBySplit<int?>(ids).ToArray();
				if (_BankrollLogService.Delete(p=>p.Status != 1 && arrIDs.Contains(p.ItemID)))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新资金状态
		/// </summary>
		/// <param name="ids"></param>
		/// <param name="MoneyStatus"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,BankrollItemList")]
		public IActionResult UpdateBankrollStatus(string ids, int MoneyStatus = 1)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "更新失败！没有指定要操作的记录ID！"
				});
			}
			try
			{
				if (_BankrollLogService.Update("Status=@MoneyStatus", " and ItemID in @ids", new { ids = StringHelper.GetArrayBySplit<int>(ids).ToArray(), MoneyStatus }))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "更新失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}

		/// <summary>
		/// 查看资金明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,BankrollItemList")]
		public IActionResult ViewBankrollItem(int id = 0)
		{
			if (id <= 0)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}
			var entity = _BankrollLogService.Get(id.ToInt32());
			if (entity == null)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}
			ViewBag.ClientName = string.Empty;
			if (entity.ClientID > 0)
			{
				var clientEntity = _ClientService.Get(entity.ClientID.ToInt32());
				if (clientEntity != null)
				{
					ViewBag.ClientName = clientEntity.ClientName;
				}
			}
			return View(entity);
		}
		#endregion

		#region 积分明细
		/// <summary>
		/// 积分明细页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ExpLog")]
		public IActionResult ExpLog()
		{
			return View();
		}
		/// <summary>
		/// 得到积分明细的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ExpLog")]
		public IActionResult GetExpLogList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			string SearchName = Utility.Query("SearchName");
			string SearchKeyword = Utility.Query("SearchKeyword");
			int TabStatus = Utility.Query("TabStatus", 0);
			string BeginDate = Utility.Query("BeginDate");
			string EndDate = Utility.Query("EndDate");
			string filter = " 1=1 ";
			if (!string.IsNullOrEmpty(SearchKeyword))
			{
				filter += " and " + SearchName + " like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%'";
			}
			if (!string.IsNullOrEmpty(BeginDate))
			{
				DateTime now;
				if (DateTime.TryParse(BeginDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',LogTime) >= 0 ";
				}
			}
			if (!string.IsNullOrEmpty(EndDate))
			{
				DateTime now;
				if (DateTime.TryParse(EndDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',LogTime) <= 0 ";
				}
			}
			switch (TabStatus)
			{
				case 1://收入
				case 2://支出
					filter += " and IncomePayout = "+ TabStatus;
					break;
			}
			string PrimaryColumn = "LogID";
			string strColumn = " * ";
			string TableName = " ExpLog ";
			string SortColumnDbType = "DateTime";
			string SortColumn = "LogTime";
			int RecordTotal;
			var result = _ExpLogService.GetListPagedBySortColumn<dynamic>(startRowIndexId: (PageNum - 1) * PageSize, maxNumberRows: PageSize,
				PrimaryColumn: PrimaryColumn, StrColumn: strColumn, TableName: TableName, Filter: filter,
				SortColumnDbType: SortColumnDbType, SortColumn: SortColumn, Sorts: "desc", out RecordTotal).ToList();
			string strSQL = "select ISNULL(SUM(ExpNum), 0) from " + TableName + " where " + filter;
			var totalIncome = _BankrollLogService.GetBySQL<int>(strSQL + " and IncomePayout=1");
			var totalPayout = _BankrollLogService.GetBySQL<int>(strSQL + " and IncomePayout=2");
			var pagerModel = new PagerModel<dynamic>(PageNum, PageSize, RecordTotal, result);
			var tempEntity = new { pagerModel.PageNum, pagerModel.PageSize, pagerModel.RecordTotal, pagerModel.PageCount, pagerModel.RowList, TotalIncome = totalIncome.ToString(), TotalPayout = totalPayout.ToString() };
			return Json(tempEntity);
		}

		/// <summary>
		/// 删除积分明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ExpLog")]
		public IActionResult DelExpLog(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if (_ExpLogService.Delete(id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除积分明细
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ExpLog")]
		public IActionResult DelExpLogMulti(string ids)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			if (!DataValidator.IsValidId(ids))
			{
				return Json(new
				{
					Result = "删除失败！指定要删除的记录ID格式不对！"
				});
			}
			try
			{
				var arrIDs = StringHelper.GetArrayBySplit<int?>(ids).ToArray();
				if (_ExpLogService.Delete(p => arrIDs.Contains(p.LogID)))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}

		/// <summary>
		/// 查看积分明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ExpLog")]
		public IActionResult ViewExpLog(int id = 0)
		{
			if (id <= 0)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}
			var entity = _ExpLogService.Get(id);
			if (entity == null)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}
			
			return View(entity);
		}
		#endregion

		#region 金币明细
		/// <summary>
		/// 金币明细页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,PointLog")]
		public IActionResult PointLog()
		{
			return View();
		}
		/// <summary>
		/// 得到金币明细的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,PointLog")]
		public IActionResult GetPointLogList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			string SearchName = Utility.Query("SearchName");
			string SearchKeyword = Utility.Query("SearchKeyword");
			int TabStatus = Utility.Query("TabStatus", 0);
			string BeginDate = Utility.Query("BeginDate");
			string EndDate = Utility.Query("EndDate");
			string filter = " 1=1 ";
			if (!string.IsNullOrEmpty(SearchKeyword))
			{
				filter += " and " + SearchName + " like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%'";
			}
			if (!string.IsNullOrEmpty(BeginDate))
			{
				DateTime now;
				if (DateTime.TryParse(BeginDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',LogTime) >= 0 ";
				}
			}
			if (!string.IsNullOrEmpty(EndDate))
			{
				DateTime now;
				if (DateTime.TryParse(EndDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',LogTime) <= 0 ";
				}
			}
			switch (TabStatus)
			{
				case 1://收入
				case 2://支出
					filter += " and IncomePayOut = " + TabStatus;
					break;
			}
			string PrimaryColumn = "LogID";
			string strColumn = " * ";
			string TableName = " PointLog ";
			string SortColumnDbType = "DateTime";
			string SortColumn = "LogTime";
			int RecordTotal;
			var result = _PointLogService.GetListPagedBySortColumn<dynamic>(startRowIndexId: (PageNum - 1) * PageSize, maxNumberRows: PageSize,
				PrimaryColumn: PrimaryColumn, StrColumn: strColumn, TableName: TableName, Filter: filter,
				SortColumnDbType: SortColumnDbType, SortColumn: SortColumn, Sorts: "desc", out RecordTotal).ToList();
			string strSQL = "select ISNULL(SUM(Point), 0) from " + TableName + " where " + filter;
			var totalIncome = _BankrollLogService.GetBySQL<int>(strSQL + " and IncomePayOut=1");
			var totalPayout = _BankrollLogService.GetBySQL<int>(strSQL + " and IncomePayOut=2");
			var pagerModel = new PagerModel<dynamic>(PageNum, PageSize, RecordTotal, result);
			var tempEntity = new { pagerModel.PageNum, pagerModel.PageSize, pagerModel.RecordTotal, pagerModel.PageCount, pagerModel.RowList, TotalIncome = totalIncome.ToString(), TotalPayout = totalPayout.ToString() };
			return Json(tempEntity);
		}

		/// <summary>
		/// 删除金币明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,PointLog")]
		public IActionResult DelPointLog(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if (_PointLogService.Delete(id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除金币明细
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,PointLog")]
		public IActionResult DelPointLogMulti(string ids)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			if (!DataValidator.IsValidId(ids))
			{
				return Json(new
				{
					Result = "删除失败！指定要删除的记录ID格式不对！"
				});
			}
			try
			{
				var arrIDs = StringHelper.GetArrayBySplit<int?>(ids).ToArray();
				if (_PointLogService.Delete(p => arrIDs.Contains(p.LogID)))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}

		/// <summary>
		/// 查看金币明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,PointLog")]
		public IActionResult ViewPointLog(int id = 0)
		{
			if (id <= 0)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}
			var entity = _PointLogService.Get(id);
			if (entity == null)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}

			return View(entity);
		}
		#endregion

		#region 有效期明细
		/// <summary>
		/// 有效期明细页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ValidLog")]
		public IActionResult ValidLog()
		{
			return View();
		}
		/// <summary>
		/// 得到有效期明细的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ValidLog")]
		public IActionResult GetValidLogList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			string SearchName = Utility.Query("SearchName");
			string SearchKeyword = Utility.Query("SearchKeyword");
			int TabStatus = Utility.Query("TabStatus", 0);
			string BeginDate = Utility.Query("BeginDate");
			string EndDate = Utility.Query("EndDate");
			string filter = " 1=1 ";
			if (!string.IsNullOrEmpty(SearchKeyword))
			{
				filter += " and " + SearchName + " like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%'";
			}
			if (!string.IsNullOrEmpty(BeginDate))
			{
				DateTime now;
				if (DateTime.TryParse(BeginDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',LogTime) >= 0 ";
				}
			}
			if (!string.IsNullOrEmpty(EndDate))
			{
				DateTime now;
				if (DateTime.TryParse(EndDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',LogTime) <= 0 ";
				}
			}
			switch (TabStatus)
			{
				case 1://收入
				case 2://支出
					filter += " and IncomePayout = " + TabStatus;
					break;
			}
			string PrimaryColumn = "LogID";
			string strColumn = " * ";
			string TableName = " ValidLog ";
			string SortColumnDbType = "DateTime";
			string SortColumn = "LogTime";
			int RecordTotal;
			var result = _ValidLogService.GetListPagedBySortColumn<dynamic>(startRowIndexId: (PageNum - 1) * PageSize, maxNumberRows: PageSize,
				PrimaryColumn: PrimaryColumn, StrColumn: strColumn, TableName: TableName, Filter: filter,
				SortColumnDbType: SortColumnDbType, SortColumn: SortColumn, Sorts: "desc", out RecordTotal).ToList();
			string strSQL = "select ISNULL(SUM(ValidNum), 0) from " + TableName + " where " + filter;
			var totalIncome = _BankrollLogService.GetBySQL<int>(strSQL + " and IncomePayout=1");
			var totalPayout = _BankrollLogService.GetBySQL<int>(strSQL + " and IncomePayout=2");
			var pagerModel = new PagerModel<dynamic>(PageNum, PageSize, RecordTotal, result);
			var tempEntity = new { pagerModel.PageNum, pagerModel.PageSize, pagerModel.RecordTotal, pagerModel.PageCount, pagerModel.RowList, TotalIncome = totalIncome.ToString(), TotalPayout = totalPayout.ToString() };
			return Json(tempEntity);
		}

		/// <summary>
		/// 删除有效期明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ValidLog")]
		public IActionResult DelValidLog(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if (_ValidLogService.Delete(id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除有效期明细
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,ValidLog")]
		public IActionResult DelValidLogMulti(string ids)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			if (!DataValidator.IsValidId(ids))
			{
				return Json(new
				{
					Result = "删除失败！指定要删除的记录ID格式不对！"
				});
			}
			try
			{
				var arrIDs = StringHelper.GetArrayBySplit<int?>(ids).ToArray();
				if (_ValidLogService.Delete(p => arrIDs.Contains(p.LogID)))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}

		/// <summary>
		/// 查看有效期明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,ValidLog")]
		public IActionResult ViewValidLog(int id = 0)
		{
			if (id <= 0)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}
			var entity = _ValidLogService.Get(id);
			if (entity == null)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}

			return View(entity);
		}
		#endregion

		#region 在线支付明细
		/// <summary>
		/// 在线支付明细列表页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,PaymentLogManage")]
		public IActionResult PaymentLogManage()
		{
			var result = _PayPlatFormService.GetList(p => p.IsDisabled == true);
			return View(result);
		}

		/// <summary>
		/// 得到在线支付明细的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,PaymentLogManage")]
		public IActionResult GetPaymentLogList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			string SearchName = Utility.Query("SearchName");
			string SearchKeyword = Utility.Query("SearchKeyword");
			int PayPlatformID = Utility.Query("PayPlatform", 0);
			int TabStatus = Utility.Query("TabStatus", 0);
			string BeginDate = Utility.Query("BeginDate");
			string EndDate = Utility.Query("EndDate");
			string filter = " 1=1 ";
			if (!string.IsNullOrEmpty(SearchKeyword))
			{
				if (SearchName == "OrderID")//订单编号
				{
					filter += " and " + SearchName + " IN (SELECT OrderID FROM Orders WHERE OrderNum like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%')";
				}
				else
				{
					filter += " and " + SearchName + " like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%'";
				}
			}
			if (!string.IsNullOrEmpty(BeginDate))
			{
				DateTime now;
				if (DateTime.TryParse(BeginDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',PayTime) >= 0 ";
				}
			}
			if (!string.IsNullOrEmpty(EndDate))
			{
				DateTime now;
				if (DateTime.TryParse(EndDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',PayTime) <= 0 ";
				}
			}
			if (PayPlatformID > 0)
			{
				filter += " and PlatformID=" + PayPlatformID;
			}
			switch (TabStatus)
			{
				case 1://未提交
				case 2://已提交
				case 3://已成功
				case 4://已失败
					filter += " and P.Status = " + TabStatus;
					break;
			}
			string PrimaryColumn = "P.PaymentLogID";
			string strColumn = " P.*,(select ISNULL(PayPlatformName,'') from PayPlatForm where PayPlatformID=P.PlatformID) as PayPlatformName,(select ISNULL(OrderNum,'') from Orders O where O.OrderID=P.OrderID) as OrderNum ";
			string TableName = "PaymentLog P";
			string SortColumnDbType = "DateTime";
			string SortColumn = "PayTime";
			int RecordTotal;
			var result = _PaymentLogService.GetListPagedBySortColumn<dynamic>(startRowIndexId: (PageNum - 1) * PageSize, maxNumberRows: PageSize,
				PrimaryColumn: PrimaryColumn, StrColumn: strColumn, TableName: TableName, Filter: filter,
				SortColumnDbType: SortColumnDbType, SortColumn: SortColumn, Sorts: "desc", out RecordTotal).ToList();
			string strSQL = "select ISNULL(SUM(MoneyTrue), 0) from " + TableName + " where " + filter;
			var totalIncome = _PaymentLogService.GetBySQL<decimal>(strSQL + " and P.Status=3");
			var pagerModel = new PagerModel<dynamic>(PageNum, PageSize, RecordTotal, result);
			var tempEntity = new { pagerModel.PageNum, pagerModel.PageSize, pagerModel.RecordTotal, pagerModel.PageCount, pagerModel.RowList, TotalIncome = totalIncome.ToString("0.00")};
			return Json(tempEntity);
		}

		/// <summary>
		/// 删除在线支付明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,PaymentLogManage")]
		public IActionResult DelPaymentLog(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if (_PaymentLogService.Get(id).Status > 1)
				{
					return Json(new
					{
						Result = "删除失败！不能删除已提交的记录！"
					});
				}
				if (_PaymentLogService.Delete(id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除在线支付明细
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,PaymentLogManage")]
		public IActionResult DelPaymentLogMulti(string ids)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			if (!DataValidator.IsValidId(ids))
			{
				return Json(new
				{
					Result = "删除失败！指定要删除的记录ID格式不对！"
				});
			}
			try
			{
				var arrIDs = StringHelper.GetArrayBySplit<int?>(ids).ToArray();
				if (_PaymentLogService.Delete(p => p.Status == 1 && arrIDs.Contains(p.PaymentLogID)))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 更新在线支付明细状态
		/// </summary>
		/// <param name="ids"></param>
		/// <param name="Status"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,PaymentLogManage")]
		public IActionResult UpdatePaymentLogStatus(string ids, int Status = 3)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "更新失败！没有指定要操作的记录ID！"
				});
			}
			try
			{
				if (_PaymentLogService.Update("Status=@Status,SuccessTime=GETDATE()", " and Status <= 2 and PaymentLogID in @ids", new { ids = StringHelper.GetArrayBySplit<int>(ids).ToArray(), Status }))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "更新失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "更新失败！" + ex.Message
				});
			}
		}

		/// <summary>
		/// 查看在线支付明细
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,PaymentLogManage")]
		public IActionResult ViewPaymentLog(int id = 0)
		{
			if (id <= 0)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}
			var entity = _PaymentLogService.Get(id.ToInt32());
			if (entity == null)
			{
				Utility.WriteErrMsg("记录不存在");
				return View();
			}
			ViewBag.PayPlatformName = string.Empty;
			if (entity.PlatformID > 0)
			{
				var payPlatformEntity = _PayPlatFormService.Get(entity.PlatformID.ToInt32());
				if (payPlatformEntity != null)
				{
					ViewBag.PayPlatformName = payPlatformEntity.PayPlatformName;
				}
			}
			ViewBag.OrderNum = string.Empty;
			if (entity.OrderID > 0)
			{
				var ordersEntity = _OrdersService.Get(entity.OrderID.ToInt32());
				if (ordersEntity != null)
				{
					ViewBag.OrderNum = ordersEntity.OrderNum;
				}
			}
			return View(entity);
		}
		#endregion

		#region 提现管理
		/// <summary>
		/// 提现管理列表页面
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,MentionManage")]
		public IActionResult MentionManage()
		{
			return View();
		}

		/// <summary>
		/// 得到提现记录的分页数据
		/// </summary>
		/// <returns></returns>
		[AdminAuthorize(Roles = "SuperAdmin,MentionManage")]
		public IActionResult GetMentionList()
		{
			int PageNum = Utility.Query("PageNum", 1);
			int PageSize = Utility.Query("PageSize", 10);
			string SearchName = Utility.Query("SearchName");
			string SearchKeyword = Utility.Query("SearchKeyword");
			int TabStatus = Utility.Query("TabStatus", -1);
			string BeginDate = Utility.Query("BeginDate");
			string EndDate = Utility.Query("EndDate");
			string filter = " 1=1 ";
			if (!string.IsNullOrEmpty(SearchKeyword))
			{
				filter += " and " + SearchName + " like '%" + DataSecurity.FilterBadChar(SearchKeyword) + "%'";
			}
			if (!string.IsNullOrEmpty(BeginDate))
			{
				DateTime now;
				if (DateTime.TryParse(BeginDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',ApplicationTime) >= 0 ";
				}
			}
			if (!string.IsNullOrEmpty(EndDate))
			{
				DateTime now;
				if (DateTime.TryParse(EndDate, out now))
				{
					filter += " and datediff(dd,'" + now.ToString("yyyy-MM-dd") + "',ApplicationTime) <= 0 ";
				}
			}
			switch (TabStatus)
			{
				case 0://已申请
				case 1://提现成功
				case 2://提现失败
					filter += " and MentionStatus = " + TabStatus;
					break;
			}
			string PrimaryColumn = "ID";
			string strColumn = " * ";
			string TableName = "MentionLog";
			string SortColumnDbType = "DateTime";
			string SortColumn = "ApplicationTime";
			int RecordTotal;
			var result = _MentionLogService.GetListPagedBySortColumn<dynamic>(startRowIndexId: (PageNum - 1) * PageSize, maxNumberRows: PageSize,
				PrimaryColumn: PrimaryColumn, StrColumn: strColumn, TableName: TableName, Filter: filter,
				SortColumnDbType: SortColumnDbType, SortColumn: SortColumn, Sorts: "desc", out RecordTotal).ToList();
			string strSQL = "select ISNULL(SUM(ApplicationAmount), 0) from " + TableName + " where " + filter;
			var totalMoney = _MentionLogService.GetBySQL<decimal>(strSQL + " and MentionStatus=1");
			var pagerModel = new PagerModel<dynamic>(PageNum, PageSize, RecordTotal, result);
			var tempEntity = new { pagerModel.PageNum, pagerModel.PageSize, pagerModel.RecordTotal, pagerModel.PageCount, pagerModel.RowList, TotalMoney = totalMoney.ToString("0.00") };
			return Json(tempEntity);
		}

		/// <summary>
		/// 删除提现记录
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,MentionManage")]
		public IActionResult DelMention(int id)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			try
			{
				if (_MentionLogService.Get(id).MentionStatus > 0)
				{
					return Json(new
					{
						Result = "删除失败！不能删除已审核的记录！"
					});
				}
				if (_MentionLogService.Delete(id))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 批量删除提现记录
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,MentionManage")]
		public IActionResult DelMentionMulti(string ids)
		{
			if (string.IsNullOrEmpty(ids))
			{
				return Json(new
				{
					Result = "删除失败！没有指定要删除的记录ID！"
				});
			}
			if (!DataValidator.IsValidId(ids))
			{
				return Json(new
				{
					Result = "删除失败！指定要删除的记录ID格式不对！"
				});
			}
			try
			{
				var arrIDs = StringHelper.GetArrayBySplit<int?>(ids).ToArray();
				if (_MentionLogService.Delete(p => p.MentionStatus <= 0 && arrIDs.Contains(p.ID)))
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "删除失败！"
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "删除失败！" + ex.Message
				});
			}
		}
		/// <summary>
		/// 提现审核。成功则扣除会员资金余额；失败则修改提现状态，并记录失败原因。
		/// </summary>
		/// <param name="id"></param>
		/// <param name="Status"></param>
		/// <returns></returns>
		[HttpPost]
		[ValidateAntiForgeryToken]
		[AdminAuthorize(Roles = "SuperAdmin,MentionManage")]
		public IActionResult UpdateMentionStatus(int id, int Status = 1)
		{
			if (id <= 0)
			{
				return Json(new
				{
					Result = "提现失败！没有指定要操作的记录ID！"
				});
			}
			try
			{
				var resultInfo = _MentionService.MentionByArtificial(id, Status);
				if (resultInfo.Status==1)
				{
					return Json(new
					{
						Result = "ok"
					});
				}
				else
				{
					return Json(new
					{
						Result = "提现失败！" + resultInfo.Msg
					});
				}
			}
			catch (Exception ex)
			{
				return Json(new
				{
					Result = "提现失败！" + ex.Message
				});
			}
		}
		#endregion
	}
}
